<?php

namespace App\Http\Middleware;

use Closure;
use Entrust;
use Illuminate\Support\Facades\Auth;

class Permission {


	/**
	 * @param          $request
	 * @param callable $next
	 * @param          $role       --角色
	 * @param          $permission --权限
	 * @return mixed
	 */
	public function handle($request, Closure $next, $role, $permission = 'all') {

		if (Auth::user()->name != env('SUPER_ADMIN')) {
			if ($role) {
				if (!Entrust::hasRole($role)) {
					return redirect('/');
				}
			}

			if ($permission != 'all') {
				if (!Entrust::can($permission)) {
					return redirect('/');
				}
			}
		}

		return $next($request);
	}
}
